from main.models import User
from main import db
from sqlalchemy.exc import InvalidRequestError
from main.utils import JSONHelper
from datetime import datetime
from sqlalchemy import or_, and_
from flask import render_template

def insert(user):
    try:
        user.time = datetime.now()
        db.session.add(user)
        db.session.commit()
        return True
    except InvalidRequestError:
        db.session.rollback()
    except Exception as e:
        print(e)
        return False


def query_username_password(username, password):
    return db.session.query(User).filter(and_(User.username == username, User.password == password)).first()


def query_username(username):
    return db.session.query(User).filter(User.username == username).first()


if __name__ == '__main__':
    user = User(username='test', password='test')
    insert(user)